// pages/user/update_blind_date.js
import { config } from "../../config/config";
import { User } from "../../model/user";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    fileList: [],
    name: '',
    contact_type: 1,
    contact: '',
    gender: 1,
    nation: '',
    region: ['请选择省', '市', '区'],
    birthday: '请选择出生日期',
    constellation_index: 0,
    constellation: ["请选择星座","白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座","摩羯座","水瓶座","双鱼座"],
    height: '',
    weight: '',
    marriage: 1,
    education: 1,
    unit: ["请选择单位性质","国企/央企/研究院","事业单位","政府/公务员","医院","民营私企","其他"],
    unit_index: 0,
    wages: ["请选择月收入","3k-5k","5k-10k","10k-15k","15k-20k","20k+"],
    wages_index: 0,
    room: 1,
    car: '',
    loan: 1,
    family: '',
    interest: '',
    selection: '',
    experience: '',
    sub_loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    });
    this.getUserInfo();
  },
  /**
   * 获取用户信息
   */
  async getUserInfo() {
    const res = await User.getUserInfo();
    this.setData({
      userInfo: res
    });
    if(res.user_auth) {
      let fileList = [];
      res.user_auth.imgs.forEach((item)=>{
        fileList.push({img_id: item.id, url: item.img.url})
      })
      const user_auth = res.user_auth;
      let constellation_index = 0;
      this.data.constellation.forEach((item,index)=>{
          if(item == user_auth.constellation) {
            constellation_index = index;
          }
      })
      let unit_index = 0;
      this.data.unit.forEach((item,index)=> {
        if (item == user_auth.unit_natures) {
          unit_index = index;
        }
      })
      let wages_index = 0;
      this.data.wages.forEach((item,index)=>{
        if (item == user_auth.monthly_income) {
          wages_index = index;
        }
      })
      let region = [user_auth.residence,'',''];
      this.setData({
        fileList: fileList,
        name: user_auth.name,
        contact_type: user_auth.contact_type,
        contact: user_auth.contact,
        gender: user_auth.gender,
        nation: user_auth.nation,
        region: region,
        birthday: user_auth.birthday,
        constellation_index: constellation_index,
        height: user_auth.height,
        weight: user_auth.weight,
        marriage: user_auth.marriage,
        education: user_auth.education,
        unit_index: unit_index,
        wages_index: wages_index,
        room: user_auth.house,
        car: user_auth.car,
        loan: user_auth.loan,
        family: user_auth.family,
        interest: user_auth.hobby,
        selection: user_auth.feeling,
        experience: user_auth.mate_selection,
      })
    }
    wx.hideLoading();
  },
  afterRead(event) {
    const { file } = event.detail;
    const self = this
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: config.apiFileUrl, // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      success(res) {
        const fileData = JSON.parse(res.data)
        // 上传完成需要更新 fileList
        const fileList = self.data.fileList;
        fileList.push({ ...file, url: fileData[0].url, img_id: fileData[0].id });
        self.setData({ fileList });
      },
    });
  },
  /**
   * 改变姓名
   */
  onChangeName(event) {
    this.setData({
      name: event.detail.value,
    })
  },
  /**
   * 点击切换联系方式类型
   */
  onChangeContactType(event) {
    this.setData({
      contact_type: Number(event.currentTarget.dataset.id)
    })
  },
  /**
   * 改变联系方式
   */
  onChangeContact(event) {
    this.setData({
      contact: event.detail.value,
    })
  },
  /**
   * 点击切换性别
   */
  onChangeGender(event) {
    this.setData({
      gender: Number(event.currentTarget.dataset.id)
    })
  },
  /**
   * 民族选择
   */
  onChangeNation(event) {
      this.setData({
        nation: event.detail.value,
      })
  },
  /**
   * 居住地选择
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 出生日期选择
   */
  bindDateChange: function(e) {
    this.setData({
      birthday: e.detail.value
    })
  },
  /**
   * 星座选择
   */
  bindPickerChange(e) {
    this.setData({
      constellation_index: e.detail.value
    })
  },
  /**
   * 身高选择
   */
  onChangeHeight(event) {
    this.setData({
      height: event.detail.value,
    })
  },
  /**
   * 体重选择
   */
  onChangeWight(e) {
    this.setData({
      weight: e.detail.value
    })
  },
  /**
   * 婚姻状态
   */
  onChangeMarriage(event) {
    this.setData({
      marriage: Number(event.currentTarget.dataset.id)
    })
  },
  /**
   * 学历信息
   */
  onChangeEducation(event) {
    this.setData({
      education: Number(event.currentTarget.dataset.id)
    })
  },
  /**
   * 单位性质
   */
  onChangeUnit(event) {
    this.setData({
      unit_index: event.detail.value
    })
  },
  /**
   * 月收入
   */
  onChangeWages(event) {
    this.setData({
      wages_index: event.detail.value
    })
  },
  /**
   * 有房
   */
  onChangeRoom(event) {
    this.setData({
      room: Number(event.currentTarget.dataset.id)
    })
  },
  /**
   * 贷款
   */
  onChangeLoan(event) {
    this.setData({
      loan: Number(event.currentTarget.dataset.id)
    })
  },
  /**
   * 是否有车
   */
  onChangeCar(event) {
    this.setData({
      car: event.detail.value,
    })
  },
  /**
   * 家庭情况
   */
  onChangeFamily(event) {
    this.setData({
      family: event.detail.value,
    })
  },
  /**
   * 兴趣爱好
   */
  onChangeInterest(event) {
    this.setData({
      interest: event.detail.value,
    })
  },
  /**
   * 感情经历
   */
  onChangeExperience(event) {
    this.setData({
      experience: event.detail.value,
    })
  },
  /**
   * 择偶要求
   */
  onChangeSelection(event) {
    this.setData({
      selection: event.detail.value,
    })
  },
  /**
   * 修改信息
   */
  updateData() {
    const data = {
      fileList: this.data.fileList,
      name: this.data.name,
      contact_type: this.data.contact_type,
      contact: this.data.contact,
      gender: this.data.gender,
      nation: this.data.nation,
      region: this.data.region,
      birthday: this.data.birthday,
      constellation: this.data.constellation[this.data.constellation_index],
      height: this.data.height,
      weight: this.data.weight,
      marriage: this.data.marriage,
      education: this.data.education,
      unit_natures: this.data.unit[this.data.unit_index],
      monthly_income: this.data.wages[this.data.wages_index],
      house: this.data.room,
      car: this.data.car,
      loan: this.data.loan,
      family: this.data.family,
      hobby: this.data.interest,
      feeling: this.data.selection,
      mate_selection: this.data.experience,
    }
    if(!data.name){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.nation){
      wx.showToast({
        title: '请输入民族',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(data.region[0] == "请选择省") {
      wx.showToast({
        title: '请选择居住地',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      if(data.region[0] == data.region[1]) {
        data.residence = data.region[0]+data.region[2];
      } else {
        data.residence = data.region[0]+data.region[1]+data.region[2];
      }
    }
    if(data.birthday == '请选择出生日期') {
      wx.showToast({
        title: '请选择出生日期',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(data.constellation == '请选择星座') {
      wx.showToast({
        title: '请选择星座',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.height){
      wx.showToast({
        title: '请输入身高',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.weight){
      wx.showToast({
        title: '请输入体重',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(data.monthly_income == '请选择单位性质') {
      wx.showToast({
        title: '请选择单位性质',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(data.unit_natures == '请选择月收入') {
      wx.showToast({
        title: '请选择月收入',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.car){
      wx.showToast({
        title: '是否有车',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.family){
      wx.showToast({
        title: '请输入家庭介绍',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.hobby){
      wx.showToast({
        title: '请输入兴趣爱好',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.feeling) {
      wx.showToast({
        title: '请输入感情经历',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!data.mate_selection) {
      wx.showToast({
        title: '请输入择偶标准',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      sub_loading: true
    })
    const self = this;
    wx.requestSubscribeMessage({
      tmplIds: ["G7qF_cb2hmR1cB66ODWTp8kYnrT4E9j4bn4LLaOw3-I"],
      async success (result){
        const res = await User.updateUserAuth(data);
        if(res) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
          self.setData({
            sub_loading: false
          })
          return false;
        } else {
          self.setData({
            sub_loading: false
          })
        }
      },
      async fail() {
        const res = await User.updateUserAuth(data);
        if(res) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
          self.setData({
            sub_loading: false
          })
          return false;
        } else {
          self.setData({
            sub_loading: false
          })
        }
      }
    })

  }
})